package com.swp.oa.sys.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import com.swp.oa.sys.dto.domian.PermissionGroupItemsDto;
import com.swp.oa.sys.dto.domian.PermissionValueDto;
import com.swp.oa.sys.dto.domian.RoleDto;
import com.swp.oa.sys.dto.page.Combo;
import com.swp.oa.sys.dto.page.EasyuiDataGridJson;
import com.swp.oa.sys.dto.page.EasyuiTreeNode;
import com.swp.oa.sys.dto.page.JsonMessage;
import com.swp.oa.sys.service.IAuthService;
import com.swp.oa.sys.service.IRoleService;
import com.swp.oa.sys.util.Pager;


@Controller
@RequestMapping(value="/role")
public class RoleController {
	
	@Autowired
	private IRoleService roleService;
	@Autowired
	private IAuthService authService;
	
	//返回角色页面
	@RequestMapping(value="/Index",method=RequestMethod.GET	)
	public String roleIndex()
	{
		return "/Role/Index";
	}
	//获取角色列表
	@RequestMapping(value = "/GetList", method = RequestMethod.POST)
	@ResponseBody
	public EasyuiDataGridJson getRoleList(int page, int rows) {
		EasyuiDataGridJson json = new EasyuiDataGridJson();
		Pager pager = roleService.getRoleList(page, rows);
		if(pager != null){
			json.setRows(pager.getRows());
			json.setTotal(pager.getTotalCount());			
		}
		return json;
	}	
	/**
	 * 返回创建系统角色页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/toCreateRole")
	public String toCreateRole()
	{
		return "/Role/CreateRole";
	}
	/**
	 * 创建系统角色
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/createRole")
	@ResponseBody
	public JsonMessage CreateRole(WebRequest request,RoleDto role)
	{
		JsonMessage jsonMsg = new JsonMessage();
		boolean flag = roleService.existRole(role);
		if(flag){
			String msg = "角色名称已存在";
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg(msg);
			jsonMsg.setObj(null);
			return jsonMsg;
		}
		try {
			roleService.save(role);
		} catch (Exception e) {
			e.printStackTrace();
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("创建系统角色出错！");
			jsonMsg.setObj(e.getMessage());
			return jsonMsg;
		}
		jsonMsg.setSuccess(true);
		jsonMsg.setMsg("创建系统角色成功！");
	    return jsonMsg;
	}

	// 返回编辑角色页面
	@RequestMapping("/toEditRole")
	public String toEditRole(Model model, String roleId) {
		model.addAttribute("roleId", roleId);
		return "/Role/EditRole";
	}

	// 返回编辑角色Form
	@RequestMapping("/editRoleForm")
	@ResponseBody
	public RoleDto editRoleForm(String roleId) {
		RoleDto role = roleService.getRoleByRoleId(roleId);
		return role;
	}

	// 编辑角色
	@RequestMapping(value = "/editRole")
	@ResponseBody
	public JsonMessage EditRole(WebRequest request, RoleDto role) {
		JsonMessage jsonMsg = new JsonMessage();
		boolean flag = roleService.existRole(role);
		if(flag){
			String msg = "角色名称已存在";
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg(msg);
			jsonMsg.setObj(null);
			return jsonMsg;
		}
		try {
			roleService.update(role);
		} catch (Exception e) {
			e.printStackTrace();
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("编辑系统角色出错！");
			jsonMsg.setObj(e.getMessage());
		}
		jsonMsg.setSuccess(true);
		jsonMsg.setMsg("编辑系统角色成功！");
		return jsonMsg;
	}

	// 删除系统角色
	@RequestMapping("/deleteRole")
	@ResponseBody
	public JsonMessage deleteRole(String roleIds) {
		String[] roleIdArray = roleIds.split(",");
		JsonMessage jsonMsg = new JsonMessage();
		try {
			roleService.delete(roleIdArray);
		} catch (Exception e) {
			e.printStackTrace();
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("删除系统角色失败!");
			jsonMsg.setObj(e.getMessage());
			return jsonMsg;
		}
		jsonMsg.setSuccess(true);
		jsonMsg.setMsg("删除系统角色成功!");
		return jsonMsg;
	}
	//返回编辑角色权限页面
	@RequestMapping(value="/toEditPermission")
	public String toEditPermission(Model model,String roleId )
	{	
//		System.out.println("toEditPermission roleId: " + roleId);
		RoleDto role = roleService.getRoleByRoleId(roleId);
		model.addAttribute("roleId", roleId);
		model.addAttribute("roleName",role.getName());
		return "/Role/EditPermission";
	}
	@RequestMapping("/tree")
	@ResponseBody
	public List<EasyuiTreeNode> tree(String roleId) {
//		System.out.println("come in getTree");
		List<EasyuiTreeNode> treeNodes = authService.getTree(roleId);
//		for (EasyuiTreeNode node : treeNodes) {
//			System.out.println(node.toString());
//			if(node.getChildren() != null){
//				for (EasyuiTreeNode children : node.getChildren()) {
//					System.out.println(children.toString());
//					
//				}
//			}
//		}
		return treeNodes;
	}
	@RequestMapping("/editRolePermission")
	@ResponseBody
	public JsonMessage editPermission(String roleId, String permissionItemId)
	{
//		System.out.println(new Date());
//		System.out.println("permissionItemId: " + permissionItemId);
//		System.out.println("roleId" + roleId);
		String[] rolePermissionItemArray = permissionItemId.split(",");
		JsonMessage jsonMsg = new JsonMessage();
		if(rolePermissionItemArray.length == 0){
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("设置角色权限失败!");
			jsonMsg.setObj("设置角色权限失败!");
			return jsonMsg;
		}
		try {
			roleService.saveRolePermissionItems(roleId, rolePermissionItemArray);
		} catch (Exception e) {
			e.printStackTrace();
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("设置角色权限失败!");
			jsonMsg.setObj(e.getMessage());
			return jsonMsg;
		}
		jsonMsg.setSuccess(true);
		jsonMsg.setMsg("设置角色权限成功！");
		return jsonMsg;
	}
	@RequestMapping("/getRoleComboData")
	@ResponseBody
	public List<Combo> getRoleComboData(String userId) {
//		System.out.println("userId: " + userId);
		List<Combo> list = roleService.getRoleComboData(userId);
		return list;
	}
}
